package com.zengde.ai_sandbox.controller;

import com.zengde.ai_sandbox.config.DaobaoCustAiConfig;
import com.zengde.ai_sandbox.config.DaobaoLCAiConfig;
import com.zengde.ai_sandbox.dto.ChatRequestDto;
import com.zengde.ai_sandbox.service.DoubaoCustomService;
import dev.langchain4j.service.TokenStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@RestController
public class DoubaoController {

    private final DoubaoCustomService doubaoCustomService;

    @Autowired
    DaobaoLCAiConfig.DaobaoAssistant doubaoAssistant;

    @Autowired
    DaobaoCustAiConfig.DaobaoCustAssistant doubaoCustAssistant;

    @Value("${my.doubao.normal.model.name}")
    private String normalModelName;

    @Value("${my.doubao.think.model.name}")
    private String thinkModelName;

    public DoubaoController(DoubaoCustomService doubaoCustomService) {
        this.doubaoCustomService = doubaoCustomService;
    }

    /**
     * 1.1 【thinking-stream-byCustom-with-doubao】
     * 深度思考、流式、自定义接入豆包、通用提示词
     * @param message
     * @return
     */
    /*@GetMapping(value = "/t-s-byCustom-withDb", produces = "text/event-stream")
    public Flux<String> thinkingStreamByCustomWithDb(@RequestParam String message) {
        doubaoCustomService.setModelName(thinkModelName);
        return doubaoCustomService.streamChat(message);
    }*/


    /**
     * 1.1 【thinking-stream-byCustom-with-doubao】
     * 深度思考、流式、自定义接入豆包、通用提示词
     * @param message
     * @return
     */
    @GetMapping(value = "/t-s-byCustom-withDb", produces = "text/event-stream")
    public Flux<String> thinkingStreamByCustomWithDb(@RequestParam String message) {
        TokenStream stream = doubaoCustAssistant.stream(message);
        return Flux.create(sink -> {
            stream.onPartialResponse(s ->
                    {
                        sink.next(s);

                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();

        });
    }


    /**
     * 1.2、【thinking-nonStream-byCustom-with-doubao】
     * 深度思考、流式、自定义接入豆包、通用提示词
     * @param message
     * @return
     */
    @GetMapping(value = "/t-ns-byCustom-withDb")
    public String thinkingNonStreamByCustomWithDb(@RequestParam String message) {
        doubaoCustomService.setModelName(thinkModelName);
        return doubaoCustomService.chat(message);
    }




    /**
     * 1.2、【thinking-nonStream-byCustom-with-doubao】
     * 深度思考、流式、自定义接入豆包、通用提示词
     * @param message
     * @return
     */
    /*@GetMapping(value = "/t-ns-byCustom-withDb")
    public Flux<String> thinkingNonStreamByCustomWithDb(@RequestParam String message) {
        TokenStream stream = doubaoCustAssistant.stream(message);
        return Flux.create(sink -> {
            stream.onPartialResponse(s ->
                    {
                        sink.next(s);

                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();

        });
    }*/




    /**
     * 2.1 【nonThinking-stream-by-custom-with-doubao】
     * 非深度思考、流式、自定义接入豆包、通用提示词
     * @param message
     * @return
     */
    @GetMapping(value = "/nt-s-byCust-withdb", produces = "text/event-stream")
    public Flux<String> nonThinkingStreamByCustomWithDb(@RequestParam String message) {
        doubaoCustomService.setModelName(normalModelName);
        return doubaoCustomService.streamChat(message);
    }


    /**
     * 2.2 【nonThinking-nonStream-by-custom-with-doubao】
     * 非深度思考、流式、自定义接入豆包、通用提示词
     * @param message
     * @return
     */
    @GetMapping(value = "/nt-ns-byCust-withdb")
    public String nonThinkingNonStreamByCustomWithDb(@RequestParam String message) {
        doubaoCustomService.setModelName(normalModelName);
        return doubaoCustomService.chat(message);
    }


    /**
     * 3.1 【nonThinkingStreamByLangWithDb】
     *通过商品标题生成AI人设
     * 这是一个冗余的接口，只是单纯为了用langchain4j而用langchain4j。或者说留了一个入口，毕竟框架一直在进化，总比自己一直维护来得简单些
     *
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/nt-s-byLan-withDb", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> nonThinkingStreamByLangWithDb(@RequestBody ChatRequestDto request, HttpServletResponse response) {
        TokenStream stream = doubaoAssistant.stream(request.getMessage());
        return Flux.create(sink -> {
            stream.onPartialResponse(s ->
                    {
                        sink.next(s);

                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();

        });
    }

    /**
     * 3.2 【nonThinking-nonStream-byLangchain-withDb】
     * 这是一个冗余的接口，因为又是思考，又非流式，所有几乎无用武之地，也就没打算投入太多时间
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/nt-ns-byLang-withDb")
    public String noThinkingNonStreamByLangWithDb(@RequestBody ChatRequestDto request, HttpServletResponse response) {
        return  doubaoAssistant.chat(request.getMessage());
    }
}